package com.tbit.main.service.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserLastPos;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.TerPosition;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LMY
 * @create 2022-05-23 11:47
 */
@Service("accountUserHistoryService")
public class AccountUserHistoryServiceImpl implements AccountUserHistoryService {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountUserLastPosService accountUserLastPosService;
    @Autowired
    private HistoryService historyService;

    @Override
    public void insert(String token, Integer accountId, Double lon, Double lat, Integer mapType) {
        AccountUser accountUser = tokenService.getAccount(token);
        /**插入轨迹表*/
        TerPosition terPosition = new TerPosition();
        terPosition.setMachineId(accountUser.getAccountUserId());
        terPosition.setMachineNO(accountUser.getPhone());
        terPosition.setAccountId(accountId);
        terPosition.setLat(lat);
        terPosition.setLon(lon);
        terPosition.setDt(DateTimeUtil.getNowTime());
        calibrateService.calTerPosition(terPosition, mapType, SysConstant.ORIGINAL);
        String redisHistory = redisService.get(RedisConstant.REDIS_KEY_ACCOUNTUSERID + accountUser.getPhone());
        AccountUserLastPos accountUserLastPos = accountUserLastPosService.getByAccountUserId(accountUser.getAccountUserId());
        if (redisHistory == null) {
            AccountUserLastPos userLastPos = new AccountUserLastPos();
            userLastPos.setAccountUserId(accountUser.getAccountUserId());
            userLastPos.setAccountId(accountId);
            userLastPos.setLat(terPosition.getLatC());
            userLastPos.setLon(terPosition.getLonC());
            userLastPos.setPointTime(DateTimeUtil.getNowTime());
            if (accountUserLastPos == null) {
                accountUserLastPosService.insert(userLastPos);
            }
            try {
                redisService.add(RedisConstant.REDIS_KEY_ACCOUNTUSERID + accountUser.getPhone(), JSONObject.toJSONString(terPosition), 10);
            } catch (JSONException e) {
                redisService.del(RedisConstant.REDIS_KEY_ACCOUNTUSERID + accountUser.getPhone());
            }

        } else {
            TerPosition terPositionRedis = JSONObject.parseObject(redisHistory, TerPosition.class);
            Integer distance = (int) GeoUtil.getDistance(new Point(terPosition.getLat(), terPosition.getLon()), new Point(terPositionRedis.getLat(), terPositionRedis.getLon()));
            /**只保存距离大于3米的轨迹*/
            if (distance > 3) {
                historyService.insert(terPosition);
                accountUserLastPosService.update(terPosition);

            }
        }

    }
}
